package by.alex.dzeshko.facebooksuperclient;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import by.alex.dzeshko.facebooksuperclient.api.ApiRequest;
import by.alex.dzeshko.facebooksuperclient.constants.FbConstants;
import by.alex.dzeshko.facebooksuperclient.http.RequestBuilder;
import by.alex.dzeshko.facebooksuperclient.service.PostService;

public class NewPostActivity extends FragmentActivity {
	
	private static final String LOG_TAG = "NewPostActivity";

	private Button mButtonPost, mButtonCancel;
	
	private EditText mEditText;

	private ProgressBar mProgressBarSending;
	
	private BroadcastReceiver mHttpServiceReceiver;
	
	private String mId;
	
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_new_post);
		mButtonCancel = (Button) findViewById(R.id.btnCancelPost);
		mButtonPost = (Button) findViewById(R.id.btnSendPost);
		mEditText = (EditText) findViewById(R.id.etNewPost);
		mProgressBarSending = (ProgressBar) findViewById(R.id.pdSending);
		mProgressBarSending.setVisibility(View.GONE);
		mId = getIntent().getStringExtra(FbConstants.ID);
		mHttpServiceReceiver = new BroadcastReceiver() {

			@Override
			public void onReceive(Context context, Intent intent) {
				String action = intent.getAction();
				if (action.equals(PostService.ACTION_ON_ERROR)) {
					mProgressBarSending.setVisibility(View.GONE);
					Toast.makeText(NewPostActivity.this, intent.getStringExtra(PostService.EXTRA_KEY_MESSAGE),Toast.LENGTH_SHORT).show();
					mButtonCancel.setEnabled(true);
					mButtonPost.setEnabled(true);
				} else if (action.equals(PostService.POST_ON_SUCCESS)) {
					mProgressBarSending.setVisibility(View.GONE);
				}
			}
		};
		IntentFilter filter = new IntentFilter();
		filter.addAction(PostService.ACTION_ON_ERROR);
		filter.addAction(PostService.POST_ON_SUCCESS);
		registerReceiver(mHttpServiceReceiver, filter);
	}
	
    @Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mHttpServiceReceiver);
	}
	
	public void onSendPostClick(View v){
		mButtonCancel.setEnabled(false);
		mButtonPost.setEnabled(false);
		mProgressBarSending.setVisibility(View.VISIBLE);
		Intent newMessageIntent = new Intent(this, PostService.class);
		RequestBuilder requestBuilder = new RequestBuilder();
		requestBuilder.setType(RequestBuilder.Type.POST);
		requestBuilder.setUrl(ApiRequest.getProfileFeedApiUrl(mId));
		requestBuilder.setMessage(mEditText.getText().toString());
		newMessageIntent.putExtra(PostService.HTTP_REQUEST_BUILDER, requestBuilder);
		startService(newMessageIntent);
		NewPostActivity.this.setResult(RESULT_OK); 
		finish();
	}
	
	public void onCancelClick(View v){
		setResult(RESULT_CANCELED);
		finish();
	}

}
